import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    private static String root = HT.Path(System.getProperty("user.dir"));
    private static Config config = NYA.getConfig();
    private static String api = "";

    public static void main(String[] args) {
        while (true) {
            String[] ids = new String[]{};
            String input = HT.Input("请输入Code：");
            Pattern pattern = Pattern.compile("^[\\d+,?]+$");
            Matcher matcher = pattern.matcher(input);
            if (matcher.matches()) {
                ids = input.split(",");
            } else {
                HT.Log("格式错误，请重新输入……");
                continue;
            }
            for (String id : ids) {
                Comic comic;
                HT.Log("提取索引 => " + id);
                try {
                    comic = NYA.getComic(config, id);
                    if (comic == null) {
                        HT.Log("提取失败，请检查数据源与代理状态……");
                        continue;
                    } else {
                        comic.show();
                    }
                } catch (IOException e) {
                    HT.Log("提取失败，请检查数据源与代理状态……");
                    continue;
                }
                HT.Log("生成文件夹 => " + id);
                try {
                    HT.Dir(System.getProperty("user.dir")+"\\"+id+"\\file\\");
                } catch (Exception e) {
                    HT.Log("生成失败，请检查文件路径是否合规……");
                    continue;
                }
                HT.Log("生成详情文件 => " + id);
                try {
                    NYA.setInfoFile(comic);
                } catch (IOException e) {
                    HT.Log("生成失败，请检查文件路径是否合规……");
                    continue;
                }
                HT.Log("开始下载 => " + id);
                NYA.doDownload(config,comic);
                HT.Log("开始打包 => " + id);
                try {
                    NYA.doPackage(comic);
                } catch (IOException e) {
                    HT.Log("打包失败，请检查文件路径是否合规……");
                    continue;
                }
                HT.Log("打包成功，进入下一个任务");
                System.out.println("====================");
            }
        }
    }
}
